<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>deploy</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        label {
            width: 20%;
            text-align: right;
            display: inline-block;
        }

        input {
            width: 70%;
            height: 27px;
            margin: 7px;
            border-radius: 7px;
        }
    </style>
    <script>
        var getTimeout = undefined;
        function submit() {
            let submitButton = document.querySelector('button');
            submitButton.disabled = 'disabled';
            let resultPara = document.querySelector('#result');
            let formData = new FormData();
            let key = document.querySelector('input[name="key"]').value;
            let deploy = document.querySelector('input[name="deploy"]').value;
            let deploys = document.querySelector('input[name="deploys"]').value;
            let test = document.querySelector('input[name="test"]').checked;
            formData.append('key', key);
            formData.append('deploy', deploy);
            formData.append('deploys', deploys);
            formData.append('test', test);
            formData.append('update', document.querySelector('input[name="update"]').files[0]);
            if(!getTimeout) {
                clearTimeout(getTimeout);
                getTimeout = undefined;
            }
            fetch('./deploy', { method: 'POST', body: formData })
                .then(response => response.text())
                .then(text => {
                    if (!key) {
                        submitButton.disabled = undefined;
                        resultPara.innerText = text;
                    } else {
                        resultPara.innerText = 'waiting for result';
                        getTimeout = window.setTimeout(getFunction, 1000, text);
                    }
                })
                .catch(error => { console.log(error); resultPara.innerText = 'error'; submitButton.disabled = undefined })
        }
        function getFunction(text) {
            let submitButton = document.querySelector('button');
            let resultPara = document.querySelector('#result');
            fetch('./deploy?key=' + text, { method: 'GET' })
                            .then(response => response.json())
                            .then(json => { resultPara.innerText = json.result; submitButton.disabled = undefined })
                            .catch(error => { console.log(error); resultPara.innerText = 'waiting again ...'; submitButton.disabled = undefined; getTimeout = window.setTimeout(getFunction, 1000, text); })
        }
    </script>
</head>

<body>
    <label>key</label>
    <input name="key" placeholder="deploy">
    <br />
    <label>deploy</label>
    <input name="deploy" placeholder="namespace service ip">
    <br />
    <label>deploys</label>
    <input name="deploys" placeholder="service [namespace=ip;]* 支持192.168.1.100-103,192.168.2.100,101">
    <br />
    <label><a href="specui.html" target="_blank">light-start</a></label>
    <button onclick="submit()" style="font-size: 18px;">submit</button>
    <input type="checkbox" name="test" checked title="test mode" style="width: auto; vertical-align: middle;">
    <input type="file" name="update" placeholder="update.tgz">
    <br />
    <p id="result"></p>
    <footer style="text-align:center">Powerd By <a href="https://gitee.com/xlongwei/deploy" target="_blank">xlongwei/deploy</a></footer>
</body>

</html>